home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / lpc / lpc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-11  |  4.4 KB  |  247 lines

  1. /*
  2.  * Copyright (c) 1983 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that this notice is preserved and that due credit is given
  7.  * to the University of California at Berkeley. The name of the University
  8.  * may not be used to endorse or promote products derived from this
  9.  * software without specific prior written permission. This software
  10.  * is provided ``as is'' without express or implied warranty.
  11.  */
  12.  
  13. #ifndef lint
  14. char copyright[] =
  15. "@(#) Copyright (c) 1983 Regents of the University of California.\n\
  16.  All rights reserved.\n";
  17. #endif /* not lint */
  18.  
  19. #ifndef lint
  20. static char sccsid[] = "@(#)lpc.c    5.5 (Berkeley) 5/5/88";
  21. #endif /* not lint */
  22.  
  23. /*
  24.  * lpc -- line printer control program
  25.  */
  26. #include <stdio.h>
  27. #include <signal.h>
  28. #include <ctype.h>
  29. #include <setjmp.h>
  30. #include <syslog.h>
  31.  
  32. #include "lpc.h"
  33.  
  34. int    fromatty;
  35.  
  36. char    cmdline[200];
  37. int    margc;
  38. char    *margv[20];
  39. int    top;
  40. int    intr();
  41. struct    cmd *getcmd();
  42. extern int NCMDS;
  43.  
  44. int debug = 0;
  45. jmp_buf    toplevel;
  46.  
  47. main(argc, argv)
  48.     char *argv[];
  49. {
  50.     register struct cmd *c;
  51.     extern char *name;
  52.  
  53.     name = argv[0];
  54.     openlog("lpd", 0, LOG_LPR);
  55.  
  56.     if (--argc > 0) {
  57.         c = getcmd(*++argv);
  58.         if (c == (struct cmd *)-1) {
  59.             printf("?Ambiguous command\n");
  60.             exit(1);
  61.         }
  62.         if (c == 0) {
  63.             printf("?Invalid command\n");
  64.             exit(1);
  65.         }
  66.         if (c->c_priv && getuid()) {
  67.             printf("?Privileged command\n");
  68.             exit(1);
  69.         }
  70.         (*c->c_handler)(argc, argv);
  71.         exit(0);
  72.     }
  73.     fromatty = isatty(fileno(stdin));
  74.     top = setjmp(toplevel) == 0;
  75.     if (top)
  76.         signal(SIGINT, intr);
  77.     for (;;) {
  78.         cmdscanner(top);
  79.         top = 1;
  80.     }
  81. }
  82.  
  83. intr()
  84. {
  85.     if (!fromatty)
  86.         exit(0);
  87.     longjmp(toplevel, 1);
  88. }
  89.  
  90. /*
  91.  * Command parser.
  92.  */
  93. cmdscanner(top)
  94.     int top;
  95. {
  96.     register struct cmd *c;
  97.  
  98.     if (!top)
  99.         putchar('\n');
  100.     for (;;) {
  101.         if (fromatty) {
  102.             printf("lpc> ");
  103.             fflush(stdout);
  104.         }
  105.         if (gets(cmdline) == 0)
  106.             quit();
  107.         if (cmdline[0] == 0)
  108.             break;
  109.         makeargv();
  110.         c = getcmd(margv[0]);
  111.         if (c == (struct cmd *)-1) {
  112.             printf("?Ambiguous command\n");
  113.             continue;
  114.         }
  115.         if (c == 0) {
  116.             printf("?Invalid command\n");
  117.             continue;
  118.         }
  119.         if (c->c_priv && getuid()) {
  120.             printf("?Privileged command\n");
  121.             continue;
  122.         }
  123.         (*c->c_handler)(margc, margv);
  124.     }
  125.     longjmp(toplevel, 0);
  126. }
  127.  
  128. extern struct cmd cmdtab[];
  129.  
  130. struct cmd *
  131. getcmd(name)
  132.     register char *name;
  133. {
  134.     register char *p, *q;
  135.     register struct cmd *c, *found;
  136.     register int nmatches, longest;
  137.     int ncmds;
  138.  
  139.     longest = 0;
  140.     nmatches = 0;
  141.     found = 0;
  142.     for (c = cmdtab, ncmds = NCMDS; --ncmds >= 0; c++) {
  143.             p = c->c_name;
  144.         for (q = name; *q == *p++; q++)
  145.             if (*q == 0)        /* exact match? */
  146.                 return(c);
  147.         if (!*q) {            /* the name was a prefix */
  148.             if (q - name > longest) {
  149.                 longest = q - name;
  150.                 nmatches = 1;
  151.                 found = c;
  152.             } else if (q - name == longest)
  153.                 nmatches++;
  154.         }
  155.     }
  156.     if (nmatches > 1)
  157.         return((struct cmd *)-1);
  158.     return(found);
  159. }
  160.  
  161. /*
  162.  * Slice a string up into argc/argv.
  163.  */
  164. makeargv()
  165. {
  166.     register char *cp;
  167.     register char **argp = margv;
  168.  
  169.     margc = 0;
  170.     for (cp = cmdline; *cp;) {
  171.         while (isspace(*cp))
  172.             cp++;
  173.         if (*cp == '\0')
  174.             break;
  175.         *argp++ = cp;
  176.         margc += 1;
  177.         while (*cp != '\0' && !isspace(*cp))
  178.             cp++;
  179.         if (*cp == '\0')
  180.             break;
  181.         *cp++ = '\0';
  182.     }
  183.     *argp++ = 0;
  184. }
  185.  
  186. #define HELPINDENT (sizeof ("directory"))
  187.  
  188. /*
  189.  * Help command.
  190.  */
  191. help(argc, argv)
  192.     int argc;
  193.     char *argv[];
  194. {
  195.     register struct cmd *c;
  196.  
  197.     if (argc == 1) {
  198.         register int i, j, w;
  199.         int columns, width = 0, lines;
  200.  
  201.         printf("Commands may be abbreviated.  Commands are:\n\n");
  202.         for (c = cmdtab; c < &cmdtab[NCMDS]; c++) {
  203.             int len = strlen(c->c_name);
  204.  
  205.             if (len > width)
  206.                 width = len;
  207.         }
  208.         width = (width + 8) &~ 7;
  209.         columns = 80 / width;
  210.         if (columns == 0)
  211.             columns = 1;
  212. #ifdef sprite
  213.         lines = (NCMDS + columns) / columns;
  214. #else
  215.         lines = (NCMDS + columns - 1) / columns;
  216. #endif
  217.         for (i = 0; i < lines; i++) {
  218.             for (j = 0; j < columns; j++) {
  219.                 c = cmdtab + j * lines + i;
  220.                 printf("%s", c->c_name);
  221.                 if (c + lines >= &cmdtab[NCMDS]) {
  222.                     printf("\n");
  223.                     break;
  224.                 }
  225.                 w = strlen(c->c_name);
  226.                 while (w < width) {
  227.                     w = (w + 8) &~ 7;
  228.                     putchar('\t');
  229.                 }
  230.             }
  231.         }
  232.         return;
  233.     }
  234.     while (--argc > 0) {
  235.         register char *arg;
  236.         arg = *++argv;
  237.         c = getcmd(arg);
  238.         if (c == (struct cmd *)-1)
  239.             printf("?Ambiguous help command %s\n", arg);
  240.         else if (c == (struct cmd *)0)
  241.             printf("?Invalid help command %s\n", arg);
  242.         else
  243.             printf("%-*s\t%s\n", HELPINDENT,
  244.                 c->c_name, c->c_help);
  245.     }
  246. }
  247.